<template>
  <view class="bg-white padding-sm">
    <uni-section title="上传图片" type="line">
      <view class="example-body">
        <uni-file-picker limit="1" title="请选择图片"
                         @select="select"
                         @progress="progress"
                         @success="success"
                         @fail="fail"

        ></uni-file-picker>
      </view>
    </uni-section>
    <uni-section title="标题" subTitle="填写标题" type="line" padding>
      <!--uni的普通单行输入框-->
      <uni-easyinput v-model="title" placeholder="请输入标题"></uni-easyinput>
    </uni-section>

    <uni-section title="详细求助内容" subTitle="填写详细求助内容" type="line" padding>
      <uni-easyinput type="textarea" maxlength="-1"  v-model="substance" placeholder="请输入内容"></uni-easyinput>
    </uni-section>
    <button type="primary" @click="onsubmit">提交</button>
  </view>
</template>

<script>
import {uploadFile} from "@/api/system/user.js"
import {publishForum} from "../../api/flower/forum";
export default {
  name: "publishForum",
  data() {
    return {
      substance:"",//详细内容
      title:"",//标题
      //图片
      imageUrl:""
    }
  },
  methods: {
    // 获取上传状态
    select(e){
      console.log('选择文件：',e);
      let that = this;
      //上传第一个图片
      let imgPath = e.tempFilePaths[0];
      uploadFile({
        filePath:imgPath,
        name:"file",
      }).then(res => {
        console.log(res);
        //console.log(res.data);
        //fileName
        that.imageUrl = res.fileName;
      });

    },
    // 获取上传进度
    progress(e){
      console.log('上传进度：',e)
    },

    // 上传成功
    success(e){
      console.log('上传成功')
    },

    // 上传失败
    fail(e){
      console.log('上传失败：',e)
    },


    onsubmit(){
      //弹出框提示 标题 内容 图片必须不能为空
      if(this.title==""||this.substance==""||this.imageUrl==""){
        uni.showToast({
          title: '请完整信息',
          icon: 'none'
        });
      }else{
        //提交数据
        console.log("提交数据")
        //调用发帖接口
        //发帖 传 title 标题 thumImg 图片  content 内容
        publishForum({
          title:this.title,
          thumImg:this.imageUrl,
          content:this.substance
        }).then(res => {
          console.log(res);
          console.log(res.data);
          //增加发帖成功的弹窗
          uni.showToast({
            title: '发帖成功',
            icon: 'none'
          });
          //跳转到上一个页面，并且刷新上一个页面
          uni.navigateBack({
            delta: 1
          });
          // //让上一个页面刷新
          // uni.$emit('refreshForumList');
        });
      }
    },
  }
}
</script>

<style lang="less" scoped>

</style>